const semver = require('semver')
let { version } = require('./package.json')
const fs = require('fs')

// 读取到package.json中的version版本

// 实现小版本的更新
// console.log('当前版本-' + version, 'major', semver.inc(version, 'major')) // 1.0.1
// console.log('当前版本-' + version, 'premajor', semver.inc(version, 'premajor'))
// console.log('当前版本-' + version, 'minor', semver.inc(version, 'minor'))
// console.log('当前版本-' + version, 'preminor', semver.inc(version, 'preminor'))
// console.log('当前版本-' + version, 'patch', semver.inc(version, 'patch'))
// console.log('当前版本-' + version, 'prepatch', semver.inc(version, 'prepatch'))
// console.log('当前版本-' + version, 'prerelease', semver.inc(version, 'prerelease'))

// 先更新小版本
let patchVersion = semver.inc(version, 'patch')
console.log('patchVersion', patchVersion)

// 更新以后，进行parse
let patchVersionObject = semver.parse(patchVersion)
console.log('patchVersionObject', patchVersionObject)

// 小版本满10. 就直接更新中版本
if (patchVersionObject && patchVersionObject.patch > 10) {
  // 更新中版本
  patchVersion = semver.inc(patchVersionObject, 'minor')
  console.log('patchVersion', patchVersion)
}

// 中版本满20，则进位
if (patchVersionObject && patchVersionObject.minor > 20) {
  patchVersion = semver.inc(patchVersionObject, 'major')
}

writePackage()

function writePackage () {
  let strem = fs.readFileSync('./package.json').toString()
  // 转换成json
  strem = JSON.parse(strem)

  strem.version = patchVersion
  // console.log('strem', strem)

  // 写入文件
  fs.writeFileSync('./package.json', JSON.stringify(strem, null, 2))

  console.log('版本更新成功')
}



